﻿using OpenBlock.Terrain;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;

namespace OpenBlock.IO
{
    public class LevelRegion
    {
        public struct ChunkDescriptor
        {
            public Vector3Int chunkIdx;
            public int chunkLength;
        }

        // a 2x4x2 structure to save chunks.
        public Vector3Int position;

        public int chunksCount;
        public ChunkDescriptor[] chunks = new ChunkDescriptor[16];

        public bool loading;
        
        public LevelRegion(string path)
        {
            var fileName = Path.GetFileName(path);
            var split = fileName.Split('.');
            position.x = int.Parse(split[1]);
            position.y = int.Parse(split[2]);
            position.z = int.Parse(split[3]);
        }

        public Task<Chunk> LoadChunkAsync(Vector3Int idx)
        {
            foreach (var chunk in chunks)
            {
                if (chunk.chunkIdx == idx)
                {
                    return Task.Run(() => new Chunk());
                }
            }
            return null;
        }


        public void SaveChunk(Chunk chunk)
        {

        }
    }
}
